package com.getword.demo;

import com.distributed.lock.Callback;
import com.distributed.lock.redis.RedisDistributedLockTemplate;
import redis.clients.jedis.JedisPool;

public class Test
{
    static JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);

    static final RedisDistributedLockTemplate template = new RedisDistributedLockTemplate(jedisPool);

    static int i = 0;

    static void fun(String lock)
    {
        template.execute(lock, 6000, new Callback()
        {//获取锁超时时间为5秒
            @Override
            public Object onGetLock()
                throws InterruptedException
            {
                //TODO 获得锁后要做的事
                System.out.println(Thread.currentThread().getName() + "----" + (++i));
                Thread.sleep(100000);
                System.out.println(Thread.currentThread().getName() + "----" + (++i));
                return null;
            }

            @Override
            public Object onTimeout()
                throws InterruptedException
            {
                //TODO 获得锁超时后要做的事
                System.out.println(Thread.currentThread().getName() + "----- timeout..");
                return null;
            }
        });
    }

    public static void main(String[] args)
        throws InterruptedException {
//        for (int i = 0; i < 10; i++)
//        {
//            new Thread(() ->
//            {
//                fun("a");
//            }).start();
//        }

        fun("123");
        Thread.sleep(7000);
        fun("123");
        System.out.println("end..");
    }
}
